package com.mine.yyb.modules.sys.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.jdbc.MineBaseDao;
import com.mine.yyb.modules.sys.dao.IRyReportDao;
import com.mine.yyb.modules.sys.vo.RyReportVO;

/**
 * <pre>
 * <一句话功能简述>
 * <功能详细描述>
 * </pre>
 * 
 * @author 姓名 工号
 * @version [版本号, 2017年6月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository(value = "iRyReportDao")
public class RyReportDaoImpl extends MineBaseDao implements IRyReportDao
{
    
    private static Logger logger = LoggerFactory.getLogger(RyReportDaoImpl.class);
    
    /**
     * <pre>
     * <一句话功能简述>
     * <功能详细描述>
     * </pre>
     *
     * @author 姓名 工号
     * @version [版本号, 2017年6月14日]
     * @param reportVO
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @Override
    public int save(RyReportVO reportVO)
        throws MineException
    {
        try
        {
            logger.info(reportVO.toString());
            
            StringBuffer sql = new StringBuffer();
            sql.append(" insert into " + RyReportVO.TABLE_NAME);
            sql.append(
                " (productName,activity,price,orderDate,orderID,shopSite,rebate,label,count,labelTo,state,createDate)");
            sql.append(
                " values(:productName,:activity,:price,:orderDate,:orderID,:shopSite,:rebate,:label,:count,:labelTo,:state,now())");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), reportVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * <功能详细描述>
     * </pre>
     *
     * @author 姓名 工号
     * @version [版本号, 2017年7月28日]
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @Override
    public void truncate()
        throws MineException
    {
        try
        {
            StringBuffer sql = new StringBuffer();
            sql.append("Truncate table " + RyReportVO.TABLE_NAME);
            
            super.execute(sql.toString());
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * <功能详细描述>
     * </pre>
     *
     * @author 姓名 工号
     * @version [版本号, 2017年6月14日]
     * @param reportVO
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @Override
    public int update(RyReportVO reportVO)
        throws MineException
    {
        try
        {
            logger.info(reportVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append(" update " + RyReportVO.TABLE_NAME);
            sql.append(" set updateDate = now()");
            if (StringUtils.isNotBlank(reportVO.getActivity()))
            {
                sql.append(" ,activity = :activity");
            }
            if (StringUtils.isNotBlank(reportVO.getProductName()))
            {
                sql.append(" ,productName = :productName");
            }
            if (reportVO.getPrice() != 0)
            {
                sql.append(" ,price = :price");
            }
            if (reportVO.getCount() != 0)
            {
                sql.append(" ,count = :count");
            }
            if (reportVO.getOrderDate() != null)
            {
                sql.append(" ,orderDate = :orderDate");
            }
            if (reportVO.getRebate() != 0)
            {
                sql.append(" ,rebate=:rebate");
            }
            if (StringUtils.isNotBlank(reportVO.getLabel()))
            {
                sql.append(" ,label=:label");
            }
            if (StringUtils.isNotBlank(reportVO.getLabelTo()))
            {
                sql.append(" ,labelTo=:labelTo");
            }
            if (StringUtils.isNotBlank(reportVO.getState()))
            {
                sql.append(" ,state=:state");
            }
            sql.append(" where orderID = :orderID");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), reportVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * <功能详细描述>
     * </pre>
     *
     * @author 姓名 工号
     * @version [版本号, 2017年6月14日]
     * @param reportVO
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @Override
    public RyReportVO queryReport(RyReportVO reportVO)
        throws MineException
    {
        try
        {
            logger.info(reportVO.toString());
            
            StringBuffer sql = new StringBuffer();
            sql.append("select id, orderDate, orderID, shopSite, price, rebate, count, label, labelTo, state ");
            sql.append("from " + RyReportVO.TABLE_NAME + " ");
            sql.append("where 1 = 1 ");
            if (StringUtils.isNotBlank(reportVO.getOrderID()))
            {
                sql.append("and orderID = :orderID ");
            }
            if (StringUtils.isNotBlank(reportVO.getLabel()))
            {
                sql.append("and label = :label ");
            }
            if (StringUtils.isNotBlank(reportVO.getLabelTo()))
            {
                sql.append("and labelTo = :labelTo ");
            }
            
            return super.queryForObject(sql.toString(), reportVO, RyReportVO.class);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * <功能详细描述>
     * </pre>
     *
     * @author 姓名 工号
     * @version [版本号, 2017年6月14日]
     * @param paramsMap
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @Override
    public List<RyReportVO> queryRyReportList(Map<String, Object> paramsMap)
        throws MineException
    {
        try
        {
            StringBuffer sql = new StringBuffer();
            sql.append("select id, orderDate, orderID, price, rebate, count, label, labelTo, state, ");
            sql.append("activity, productName, price, count ");
            sql.append("from " + RyReportVO.TABLE_NAME + " ");
            sql.append("where 1 = 1 ");
            if (paramsMap.get("orderID") != null && StringUtils.isNotBlank(paramsMap.get("orderID").toString()))
            {
                sql.append("and orderID = :orderID ");
            }
            if (paramsMap.get("lastDate") != null && StringUtils.isNotBlank(paramsMap.get("lastDate").toString()))
            {
                sql.append("and orderDate > :lastDate ");
            }
            
            logger.info(sql.toString());
            return super.queryForList(sql.toString(), paramsMap, RyReportVO.class);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
}
